#!/usr/bin/env bash

CACHE_DIR="/srv/cache/obs/tar_scm/repo/next/openEuler"

MANIFEST=$1
EXITONFAILUE=$2
REVERSE_DELETE=$3

touch ${CACHE_DIR}/updateprojects_log
# for d in `ls ${CACHE_DIR}`; do
for d in `cat ${MANIFEST}`; do
    if [ ${d} = "source_kernel" ]; then
        cd /srv/cache/obs/tar_scm/repo/next/openEuler_kernel/kernel
        git pull
        if [ $? -ne 0 ]; then
            echo "${d} update failed" >> ${CACHE_DIR}/updateprojects_log
        fi
    else
        set +e
        # grep -q ${MANIFEST} -e "<project path=\"openEuler\/${d}\""
        cd ${CACHE_DIR}
        # djcount=`ls ${d}|wc -l`
        cd ${d}
        echo "starting to update code in dir: ${CACHE_DIR}/${d}";
        if [[ $EXITONFAILUE == "true" ]]; then
            set -e   
        fi
        if [[ $REVERSE_DELETE == "true" ]]; then
            git fetch --all
            git reset --hard origin/master
            git pull
            if [ $? -ne 0 ]; then
                echo "${d} update failed" >> ${CACHE_DIR}/updateprojects_log
            fi
        else
            git pull
            if [ $? -ne 0 ]; then
		echo "------------------------------${d}-----------------------------"
		cd ${CACHE_DIR}/../openTmp/
		git clone https://gitee.com/src-openeuler/${d}.git
		if [ $? -ne 0 ]; then
                    echo "${d} update failed" >> ${CACHE_DIR}/updateprojects_log
                fi
		cd ${CACHE_DIR}
	    fi
        fi
    fi
done
cat ${CACHE_DIR}/updateprojects_log
rm -rf ${CACHE_DIR}/updateprojects_log
